﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    /// <summary>
    /// 流程流转情况控件
    /// </summary>
    public partial class ProcessStepsControl : UserControl
    {
        private EventHandler m_resizeHandler;

        public ProcessStepsControl()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(ProcessStepPanelLoaded);
            this.Unloaded += new RoutedEventHandler(ProcessStepPanelUnloaded);
        }

        #region Control Event Handlers

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ProcessStepPanelLoaded(object sender, RoutedEventArgs e)
        {
            this.Width = Application.Current.Host.Content.ActualWidth * 0.5;
            this.Height = (Application.Current.Host.Content.ActualHeight * 0.8) - 150;

            m_resizeHandler = new EventHandler(ContentResized);
            Application.Current.Host.Content.Resized += m_resizeHandler;
        }

        void ProcessStepPanelUnloaded(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.Resized -= m_resizeHandler;
        }
        
        #endregion

        /// <summary>
        /// 通过WebService获取记录的流转情况
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="tableName"></param>
        /// <param name="recordID"></param>
        public void LoadSteps(string userName, string tableName, int recordID)
        {
            DynamicWebServiceSoapClient client = DynamicModule.CreateProxy();
            client.GetWorkflowRecordsCompleted += new EventHandler<GetWorkflowRecordsCompletedEventArgs>(client_GetWorkflowRecordsCompleted);
            client.GetWorkflowRecordsAsync(Guid.Empty, Guid.Empty);
        }

        /// <summary>
        /// 获取流转情况后绑定列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_GetWorkflowRecordsCompleted(object sender, GetWorkflowRecordsCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                m_listBox.ItemsSource = e.Result.Data;
            }
        }

        /// <summary>
        /// 页面根据内容自动调节大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ContentResized(object sender, EventArgs e)
        {
            this.Width = Application.Current.Host.Content.ActualWidth * 0.5;
            this.Height = (Application.Current.Host.Content.ActualHeight * 0.8) - 150;
        }
    }
}
